package cool.doudou.doudada.iot.codec.core.processor;

import cool.doudou.doudada.iot.codec.annotation.BundleStateChange;
import cool.doudou.doudada.iot.codec.core.factory.MethodFactory;
import cool.doudou.doudada.iot.codec.core.method.BundleStateChangeMethod;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

import java.util.Arrays;

/**
 * BundleStateChangeBeanPostProcessor
 *
 * @author jiangcs
 * @since 2022/09/29
 */
public class BundleStateChangeBeanPostProcessor implements BeanPostProcessor {
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        Arrays.stream(bean.getClass().getDeclaredMethods())
                .filter(method -> method.isAnnotationPresent(BundleStateChange.class))
                .forEach(method -> {
                    MethodFactory.set(BundleStateChangeMethod.builder().bean(bean).method(method).build());
                });
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        return bean;
    }
}
